<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="Content-Language" content="zh-CN"><link href="stylesheet.css" media="all" rel="stylesheet" type="text/css">
<title>外键</title>
<script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?d286c55b63a3c54a1e43d10d4c203e75"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script>
</head><body class="SECT1">
<div>
<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><th colspan="5" align="center" valign="bottom">PostgreSQL 8.2.3 中文文档</th></tr>
<tr><td width="10%" align="left" valign="top"><a href="tutorial-views.html" accesskey="P">后退</a></td><td width="10%" align="left" valign="top"><a href="tutorial-advanced.html">快退</a></td><td width="60%" align="center" valign="bottom">章3. 高级特性</td><td width="10%" align="right" valign="top"><a href="tutorial-advanced.html">快进</a></td><td width="10%" align="right" valign="top"><a href="tutorial-transactions.html" accesskey="N">前进</a></td></tr>
</table>
<hr align="LEFT" width="100%"></div>
<div class="SECT1"><h1 class="SECT1"><a name="TUTORIAL-FK">3.3. 外键</a></h1><a name="AEN946"></a><a name="AEN948"></a>
<p>回忆一下<a href="tutorial-sql.html">章2</a>里的 <code class="CLASSNAME">weather</code> 和 <code class="CLASSNAME">cities</code> 表。考虑下面的问题：你想确保没有人可以在 <code class="CLASSNAME">weather</code> 表里插入一条在 <code class="CLASSNAME">cities</code> 表里没有匹配记录的数据行。这就叫维护表的<i class="FIRSTTERM">参照完整性</i>。在简单的数据库系统里，实现(如果也叫实现)这个特性的方法通常是先看看 <code class="CLASSNAME">cities</code> 表里是否有匹配的记录，然后插入或者拒绝新的 <code class="CLASSNAME">weather</code> 记录。这个方法有许多问题，而且非常不便，因此 PostgreSQL 可以为你做这些。</p>
<p>新的表声明看起来会像下面这样：</p>
<pre class="PROGRAMLISTING">CREATE TABLE cities (
        city     varchar(80) primary key,
        location point
);

CREATE TABLE weather (
        city      varchar(80) references cities(city),
        temp_lo   int,
        temp_hi   int,
        prcp      real,
        date      date
);</pre>
<p>然后我们试图插入一条非法的记录：</p>
<pre class="PROGRAMLISTING">INSERT INTO weather VALUES ('Berkeley', 45, 53, 0.0, '1994-11-28');</pre>
<pre class="SCREEN">ERROR:  insert or update on table "weather" violates foreign key constraint "weather_city_fkey"
DETAIL:  Key (city)=(Berkeley) is not present in table "cities".</pre>
<p>外键的行为可以根据你的应用仔细调节。在这份教程里我们就不再多说了，请你参考<a href="ddl.html">章5</a>以获取更多的信息。正确使用外键无疑将改进你的数据库应用，所以我们强烈建议你学习它们。</p>
</div>
<div>
<hr align="LEFT" width="100%">
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td width="33%" align="left" valign="top"><a href="tutorial-views.html" accesskey="P">后退</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">首页</a></td><td width="33%" align="right" valign="top"><a href="tutorial-transactions.html" accesskey="N">前进</a></td></tr>
<tr><td width="33%" align="left" valign="top">视图</td><td width="34%" align="center" valign="top"><a href="tutorial-advanced.html" accesskey="U">上一级</a></td><td width="33%" align="right" valign="top">事务</td></tr>
</table>
</div>
</body></html>